﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Nissen_Common;
using Nissen_Model;
using Nissen_Resources;
using PosHacker;
using PosHacker.Form;
using PosHacker.PosException;

namespace Nissen_Manage.Forms
{
	public partial class MarkableQuestionsManage : Form
	{
		
		public POSHacker vPOSHacker { get; set; }
		private bool _closeFlag = false;
		public FormSetCourse FormSetCourse { get; set; }
		private DataGridViewCheckBoxHeaderCellHelper _checkBoxHeaderCellHelper;

		private IList<string> selectMarkID = new List<string>();
		public MarkableQuestionsManage(FormSetCourse formSetCourse,POSHacker poshacker)
		{
			InitializeComponent();
			vPOSHacker = poshacker;
			this.vPOSHacker.SetTestCourseInfo(formSetCourse);
			FormSetCourse = formSetCourse;

			_checkBoxHeaderCellHelper = new DataGridViewCheckBoxHeaderCellHelper(Column1);
			_checkBoxHeaderCellHelper.AddChekBoxHeaderForGridViewColumn();
	
		}

		private void MarkableQuestionsManage_Load(object sender, EventArgs e)
		{
			try
			{
				_closeFlag = true;
				this.lblShow.Text = FormSetCourse.subjectCourseNm +"-"+FormSetCourse.pageNum;
				this.SetLoadingPic(true);
				LoadDBData();
				Thread t = new Thread(Init);
				
				t.Start();
			}
			catch (Exception)
			{
				MessageBox.Show(ResourceCommon.GetResource("LoadError"));
				//throw;
			}
			
		}
		private void LoadDBData()
		{

			MarkableQuestionService markableQuestionService = new MarkableQuestionService();

			IList<MarkableQuestion> markableQuestions = markableQuestionService.GetMarkableQuestions(FormSetCourse.subjectCourseTestId, FormSetCourse.pageNum);

			foreach (var markableQuestion in markableQuestions)
			{
				selectMarkID.Add(markableQuestion.MarkID);
			}
			BindingDgvDBMarkableQuestions(markableQuestions);
		}
		private void Init()
		{

			try
			{
				
				List<FormSetScoringNo> formSetScoringNoes = this.vPOSHacker.GetMarkableQuestionsList();

				IList<MarkableQuestion> markableQuestions = new List<MarkableQuestion>();
                
				foreach (var formSetScoringNo in formSetScoringNoes)
				{
					MarkableQuestion markableQuestion = new MarkableQuestion();

					markableQuestion.MarkID = formSetScoringNo.scoringNo;
					//markableQuestion.MarkName = formSetScoringNo.
					markableQuestion.SubjectCourseID = FormSetCourse.subjectCourseTestId;
					markableQuestion.SubjectCoursePage = FormSetCourse.pageNum;

					if (selectMarkID.Contains(markableQuestion.MarkID))
					{
						markableQuestion.DisplayFlag = true;
					}
					markableQuestions.Add(markableQuestion);
				}

				this.Invoke(new Action<IList<MarkableQuestion>>(BindingDgvMarkableQuestions), markableQuestions);
				_closeFlag = false;
				this.Invoke(new Action<bool>(SetLoadingPic), false);

                //this.webBrowser1.Url = new Uri(@"http://pos.toshin.com/MarkingSystemWeb/displayImage.do?imageType=correctImg");//20120411 pcj
                this.webBrowser1.Navigate(Application.StartupPath + "\\AnswerDisplay.htm");//20120414 pcj
			}
			catch (PosException e)
			{
				//this.Invoke();
				MessageBox.Show(e.Message);
			}

		}
		


		private void BindingDgvMarkableQuestions(IList<MarkableQuestion> markableQuestions)
		{

			this.dgvMarkableQuestions.AutoGenerateColumns = false;
			this.dgvMarkableQuestions.DataSource = markableQuestions;

		}
		private void BindingDgvDBMarkableQuestions(IList<MarkableQuestion> markableQuestions)
		{

			this.dgvDBMarkableQuestions.AutoGenerateColumns = false;
			this.dgvDBMarkableQuestions.DataSource = markableQuestions;

		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				IList<MarkableQuestion> markableQuestions = GetSelectSubjectCourse();

				MarkableQuestionService markableQuestionService = new MarkableQuestionService();

				markableQuestionService.SaveMarkableQuestions(markableQuestions, FormSetCourse.subjectCourseTestId, FormSetCourse.pageNum);

				MessageBox.Show(ResourceCommon.GetResource("SaveSuccess"));
				LoadDBData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ResourceCommon.GetResource("SaveFail"));
				//MessageBox.Show(ex.Message);
			}
		}

		/// <summary>
		/// 获取选择数据
		/// </summary>
		/// <returns></returns>
		private IList<MarkableQuestion> GetSelectSubjectCourse()
		{
			IList<MarkableQuestion> subjectCourses = new List<MarkableQuestion>();

			for (int i = 0; i < dgvMarkableQuestions.Rows.Count; i++)
			{

				if (dgvMarkableQuestions.Rows[i].Cells[0].Value != null)
				{
					if (dgvMarkableQuestions.Rows[i].Cells[0].Value.ToString() == "True")
					{
						MarkableQuestion subjectCourse = (MarkableQuestion)dgvMarkableQuestions.Rows[i].DataBoundItem;
						
						subjectCourses.Add(subjectCourse);
					}
				}
			}
			return subjectCourses;
		}

		private void MarkableQuestionsManage_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (_closeFlag)
			{
				e.Cancel = true;
				MessageBox.Show(ResourceCommon.GetResource("LoadingNotClose"));
			}
		
		}

		private void SetLoadingPic(bool flag)
		{
			this.pictureBox1.Visible = flag;
		}

        private void button1_Click(object sender, EventArgs e)
        {
            var vform = new FormAnswerPaperSelect();
            vform.SubjectCourseId = this.FormSetCourse.subjectCourseTestId;
            vform.PageNumber = this.FormSetCourse.pageNum;
            vform.ShowDialog();
        }
	}
}
